package com.lz.ionicbase.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lz.ionicbase.R;
import com.lzy.okgo.OkGo;


/**
 * Created by lenovo on 2017/2/16.
 * 任柏全 自定义progressDialog
 */

public class LoadingDialog implements DialogInterface.OnKeyListener{

    private static ImageView imgLoad;
    private static LinearLayout layoutLoading;
    private static TextView tvLoad;

    public static Dialog createLoadingDialog(Context context, String msg) {

        // 首先得到整个View
        View view = LayoutInflater.from(context).inflate(R.layout.utils_progress, null);
        imgLoad = (ImageView) view.findViewById(R.id.img_load);
        layoutLoading = (LinearLayout) view.findViewById(R.id.layout_loading);
        tvLoad = (TextView) view.findViewById(R.id.tv_load);
        // 加载动画，动画用户使img图片不停的旋转
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.loading_anim);
        // 显示动画
        imgLoad.startAnimation(animation);
        // 显示文本
        tvLoad.setText(msg);
        // 创建自定义样式的Dialog
        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);
        loadingDialog.setContentView(layoutLoading, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        return loadingDialog;
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            //取消所有网络请求
            OkGo.getInstance().cancelAll();
            return true;
        }
        return false;
    }
}
